#include <stdio.h>
#include <stdlib.h>

struct ListNode 
{
    int val;
    struct ListNode *next;
};

struct ListNode *createList(int a[], int n);

struct ListNode *sortList(struct ListNode *head);

void output(struct ListNode *head);

struct ListNode *insertNode(struct ListNode *head, int n);

struct ListNode *removeNode(struct ListNode *head, int n);

// 函数实现

struct ListNode *createList(int arr[], int n) 
{
    // 0 == n
    if (0 == n)
    {
        return NULL;
    }

    // 定义哑节点
    struct ListNode*dummy = (struct ListNode*)malloc(sizeof(struct ListNode));
    dummy ->next = NULL;
    struct ListNode*temp = dummy;
    

    for (int i = 0;i < n;i++)
    {
        struct ListNode*node = (struct ListNode*)malloc(sizeof(struct ListNode));
        node->val = arr[i];
        node->next = NULL;

        temp->next = node;
        temp = node;
    }

    return dummy->next;
}

int compare(const void*pa,const void *pb)
{
    return (*(int*)pa-*(int*)pb);
}

struct ListNode *sortList(struct ListNode *head) 
{
    // 首先判空
    if (!head)
    {
        return head;
    }
    // 首先获取长度并创建数组
    int len = 0;
    struct ListNode*temp = head;
    while (temp)
    {
        len ++;
        temp = temp->next;
    }

    // 序列化
    int arr[len];
    temp = head;
    int i = 0;
    while (temp)
    {
        arr[i] = temp->val;
        i++;
        temp = temp->next;
    }

    // 使用qsort进行排序
    qsort(arr,len,sizeof(int),compare);
    temp = head;
    i = 0;
    while (temp)
    {
        temp->val = arr[i];
        i++;
        temp = temp->next;
    }

    // 由于没有对节点的值以外的部分产生影响
    return head;
}

void output(struct ListNode *head) 
{
    if (!head)
    {
        return ;
    }

    struct ListNode* temp = head;
    while (temp) // i++ ++i
    {
        printf("%d\n",temp->val);
        temp = temp->next;
    }
}

struct ListNode *insertNode(struct ListNode *head, int data) 
{
    struct ListNode*node = (struct ListNode*)malloc(sizeof(struct ListNode));
    node->val = data;
    node->next = NULL;

    struct ListNode*dummy = (struct ListNode*)malloc(sizeof(struct ListNode));
    dummy->next = head;

    struct ListNode*temp = dummy;
    while (temp)
    {
        if (data<=temp->next->val&&data>=temp->val)
        {
            struct ListNode*n = temp->next;
            temp->next = node;
            node->next = n;
            break;
        }
        temp = temp->next;
    }

    // temp到最后了
    if (!temp)
    {
        temp = node;
    }

    return dummy->next;
}

struct ListNode *removeNode(struct ListNode *head, int target) 
{
    struct ListNode*dummy = (struct ListNode*)malloc(sizeof(struct ListNode));
    dummy->next = head;

    // 查找删除
    struct ListNode*temp = head;
    struct ListNode*prev = dummy;
    while (temp)
    {
        if (target == temp->val)
        {
            // 找到
            prev->next = temp->next;
            free(temp);
            temp = NULL;
            break;
        }
        prev = prev->next;
        temp = temp->next;
    }

    return dummy->next;
}


int main() 
{
    int a[10000];
    int n, ins, rm;
    scanf("%d", &n);
    scanf("%d", &ins);
    scanf("%d", &rm);
    for (int i = 0; i < n; i++) 
    {
        scanf("%d", &a[i]);
    }
    struct ListNode *list = createList(a, n);
    output(list);
    list = sortList(list);
    output(list);
    list = insertNode(list, ins);
    output(list);
    list = removeNode(list, rm);
    output(list);
    return 0;
}

